// pages/ACMedicine/ACMedicine.js
Page({
  data: {
    medicineInfo: {
      medicineDossage: '',
      medicineId: '',
      medicineLot: '',
      medicineName: '',
      medicineNote: '',
      medicinePrice: '',
      medicineSum: '',
      medicineUnit: '',
      medicineUnitBig:'',
    },
    isChange: false, //是否是修改，如果是修改，在页面跳转进来时改成true,并从数据库拿数据
  },
  //页面刚进来时(用来判断时修改还是添加)
  onLoad(options) {
    var that = this
    console.log('从管理药品信息页面进来时带的药品id', options.medicineId)
    //如果是修改
    if (options.medicineId) {
      // isChange改成true
      that.setData({
        isChange: true,
      })
      //从数据库拿数据
      wx.cloud.callFunction({
        name: "medicineInfo",
        data: {
          action: "findOne",
          medicineId: options.medicineId
        }
      }).then(res => {
        console.log('拿数据库的数据成功', res)
        //把数据库拿到的数据存放到本地
        console.log(res.result.data)
        that.setData({
          medicineInfo: res.result.data[0]
        })
      }).catch(res => {
        console.log('拿数据库的数据失败', res)
      })
    }
  },

  //获取输入的值
  getmedicineName(e) {
    var medicineName = "medicineInfo.medicineName"
    this.setData({
      [medicineName]: e.detail.value
    })
  },
  getmedicineId(e) {
    var medicineId = "medicineInfo.medicineId"
    this.setData({
      [medicineId]: e.detail.value
    })
  },
  getmedicineLot(e) {
    var medicineLot = "medicineInfo.medicineLot"
    this.setData({
      [medicineLot]: e.detail.value
    })
  },
  getmedicineDossage(e) {
    var medicineDossage = "medicineInfo.medicineDossage"
    this.setData({
      [medicineDossage]: e.detail.value
    })
  },
  getmedicineUnit(e) {
    var medicineUnit = "medicineInfo.medicineUnit"
    this.setData({
      [medicineUnit]: e.detail.value
    })
  },
  getmedicineUnitBig(e) {
    var medicineUnitBig = "medicineInfo.medicineUnitBig"
    this.setData({
      [medicineUnitBig]: e.detail.value
    })
  },
  getmedicinePrice(e) {
    var medicinePrice = "medicineInfo.medicinePrice"
    this.setData({
      [medicinePrice]: e.detail.value
    })
  },
  getmedicineSum(e) {
    var medicineSum = "medicineInfo.medicineSum"
    this.setData({
      [medicineSum]: e.detail.value
    })
  },
  getmedicineNote(e) {
    var medicineNote = "medicineInfo.medicineNote"
    this.setData({
      [medicineNote]: e.detail.value.replace(/\s+/g, '')
    })
  },

  //提交
  commitA() {
    var that = this
    wx.showLoading({
      title: '药品新建中...',
    })
    //验证
    wx.cloud.callFunction({
      name: 'medicineInfo',
      data: {
        action: 'findOne',
        medicineId: that.data.medicineInfo.medicineId
      }
    }).then(res => {
      console.log('用这个药品id拿到的数据',res.result.data.length)
      if(res.result.data.length > 0){
        console.log('这个id存在了，不能新建这个id')
        wx.showToast({
          icon: 'none',
          title: '此id已被使用，请重新输入Id',
        })
        return
      }
      //其他验证2
      if (!that.data.medicineInfo.medicineName) {
        wx.showToast({
          icon: 'none',
          title: '药品名称不能为空',
        })
        return
      }
      if (!that.data.medicineInfo.medicineId) {
        wx.showToast({
          icon: 'none',
          title: '药品Id不能为空',
        })
        return
      }
      if (!that.data.medicineInfo.medicineLot) {
        wx.showToast({
          icon: 'none',
          title: '药品批次不能为空',
        })
        return
      }
      if (!that.data.medicineInfo.medicineDossage) {
        wx.showToast({
          icon: 'none',
          title: '药品剂量不能为空',
        })
        return
      }
      if (!that.data.medicineInfo.medicineUnit) {
        wx.showToast({
          icon: 'none',
          title: '剂量单位不能为空',
        })
        return
      }
      if (!that.data.medicineInfo.medicineUnitBig) {
        wx.showToast({
          icon: 'none',
          title: '药品单位不能为空',
        })
        return
      }
      if (!that.data.medicineInfo.medicinePrice) {
        wx.showToast({
          icon: 'none',
          title: '药品单价不能为空',
        })
        return
      }
      if (!that.data.medicineInfo.medicineSum) {
        wx.showToast({
          icon: 'none',
          title: '药品库存不能为空',
        })
        return
      }
      //验证完之后实现add新建3
      wx.cloud.callFunction({
        name: 'medicineInfo',
        data: {
          action: 'add',
          medicineId:that.data.medicineInfo.medicineId,
          medicineDossage: parseFloat(that.data.medicineInfo.medicineDossage),
          medicineLot: that.data.medicineInfo.medicineLot,
          medicineName: that.data.medicineInfo.medicineName,
          medicineNote: that.data.medicineInfo.medicineNote,
          medicinePrice: parseFloat(that.data.medicineInfo.medicinePrice),
          medicineSum: parseInt(that.data.medicineInfo.medicineSum,10),
          medicineUnit: that.data.medicineInfo.medicineUnit,
          medicineUnitBig: that.data.medicineInfo.medicineUnitBig,
        }
      }).then(res=>{
        console.log('新建药品成功',res)
        wx.hideLoading({
          success: (res) => {
            wx.showToast({
              title: '新建药品成功',
            })
            setTimeout(function () {
              wx.navigateBack({})
            }, 1000)
          },
        })
        
      }).catch(res=>{
        console.log('新建药品失败',res)
      })
    }).catch(res => {
      console.log('获取云数据库的medicineId出错', res)
      wx.hideLoading({
        success: (res) => {
          wx.showToast({
            icon: 'none',
            title: '新建药品失败',
          })
        },
      })
    })
  },
  //修改
  commitC() {
    var that = this
    wx.showLoading({
      title: '药品修改中...',
    }) 
     //其他验证2
     if (!that.data.medicineInfo.medicineName) {
      wx.showToast({
        icon: 'none',
        title: '药品名称不能为空',
      })
      return
    }
    if (!that.data.medicineInfo.medicineId) {
      wx.showToast({
        icon: 'none',
        title: '药品Id不能为空',
      })
      return
    }
    if (!that.data.medicineInfo.medicineLot) {
      wx.showToast({
        icon: 'none',
        title: '药品批次不能为空',
      })
      return
    }
    if (!that.data.medicineInfo.medicineDossage) {
      wx.showToast({
        icon: 'none',
        title: '药品剂量不能为空',
      })
      return
    }
    if (!that.data.medicineInfo.medicineUnit) {
      wx.showToast({
        icon: 'none',
        title: '剂量单位不能为空',
      })
      return
    }
    if (!that.data.medicineInfo.medicineUnitBig) {
      wx.showToast({
        icon: 'none',
        title: '药品单位不能为空',
      })
      return
    }
    if (!that.data.medicineInfo.medicinePrice) {
      wx.showToast({
        icon: 'none',
        title: '药品单价不能为空',
      })
      return
    }
    if (!that.data.medicineInfo.medicineSum) {
      wx.showToast({
        icon: 'none',
        title: '药品库存不能为空',
      })
      return
    }
    //验证完之后实现change修改
    wx.cloud.callFunction({
      name: 'medicineInfo',
      data: {
        action: 'change',
        medicineId:that.data.medicineInfo.medicineId,
        medicineDossage: parseFloat(that.data.medicineInfo.medicineDossage),
        medicineLot: that.data.medicineInfo.medicineLot,
        medicineName: that.data.medicineInfo.medicineName,
        medicineNote: that.data.medicineInfo.medicineNote,
        medicinePrice: parseFloat(that.data.medicineInfo.medicinePrice),
        medicineSum: parseInt(that.data.medicineInfo.medicineSum,10),
        medicineUnit: that.data.medicineInfo.medicineUnit,
        medicineUnitBig: that.data.medicineInfo.medicineUnitBig,
      }
    }).then(res=>{
      console.log('修改药品成功',res)
      wx.hideLoading({
        success: (res) => {
          wx.showToast({
            title: '修改药品成功',
          })
          setTimeout(function () {
            wx.navigateBack({})
          }, 1000)
        },
      })
      
    }).catch(res=>{
      console.log('修改药品失败',res)
    })
  }
})